﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Controllers
{
    public class ScheduleController : Controller
    {
        private ScheduleBLO _scheduleBLO = new ScheduleBLO();
        private FilmBLO _filmBLO = new FilmBLO();
        private TheaterBLO _theaterBLO = new TheaterBLO();
        private RoomBLO _roomBLO = new RoomBLO();
        private ChairBLO _chairBLO = new ChairBLO();
        private ShowTimeBLO _showtimeBLO = new ShowTimeBLO();
        private PromotionBLO _promotionBLO = new PromotionBLO();
        private UserBLO _userBO = new UserBLO();

        #region --- Booking Film ---

        /// <summary>
        /// Load all schedule for choose film, theater, showtime for booking ticket
        /// </summary>
        /// <returns></returns>
        /// <author>VuongVT</author>
        public ActionResult Index()
        {
            var listFilm = _scheduleBLO.GetListOfFilmShowing();
            List<Theater> listTheaterOfFirstFilm = new List<Theater>();
            if (listFilm.Count > 0)
            {
                listTheaterOfFirstFilm = _scheduleBLO.GetListTheaterWhenChooseFilm(listFilm[0].FilmId);
            }
            ViewBag.ListTheater = listTheaterOfFirstFilm;

            List<ShowTime> listShowTime = new List<ShowTime>();
            if (listTheaterOfFirstFilm.Count > 0)
            {
                listShowTime = _scheduleBLO.GetListShowTimeWhenChooseTheater(listTheaterOfFirstFilm[0].TheaterId, listFilm[0].FilmId);
            }
            ViewBag.ListShowTime = listShowTime;
            return View(listFilm);
        }

        /// <summary>
        /// Partial show list of Theater
        /// </summary>
        /// <param name="filmId"></param>
        /// <returns></returns>
        /// <author>vuongvt</author>
        public PartialViewResult TheatersByFilm(int filmId)
        {
            var listTheater = _scheduleBLO.GetListTheaterWhenChooseFilm(filmId);

            SearchSchedule searchSchedule = new SearchSchedule();
            searchSchedule.FilmId = filmId;
            this.Session["SearchSchedule"] = searchSchedule;

            return PartialView(listTheater);
        }

        /// <summary>
        /// Partial show list of ShowTime
        /// </summary>
        /// <param name="theaterId"></param>
        /// <returns></returns>
        /// <author>vuongvt</author>
        public PartialViewResult ShowTimeOfTheater(int theaterId)
        {
            SearchSchedule searchSchedule = new SearchSchedule();
            searchSchedule = this.Session["SearchSchedule"] as SearchSchedule;
            searchSchedule.TheaterId = theaterId;
            this.Session["SearchSchedule"] = searchSchedule;
            var listShowTime = _scheduleBLO.GetListShowTimeWhenChooseTheater(theaterId, searchSchedule.FilmId);
            return PartialView(listShowTime);
        }

        /// <summary>
        /// Redirect to CheckOut to booking a film ticket
        /// </summary>
        /// <param name="showtimeId"></param>
        /// <returns></returns>
        /// <author>vuongvt</author>
        public ActionResult BookingAFilmTicket(int showtimeId)
        {
            string userName = "VuongVT";
            SearchSchedule searchSchedule = new SearchSchedule();
            searchSchedule = this.Session["SearchSchedule"] as SearchSchedule;
            searchSchedule.ShowTimeId = showtimeId;
            searchSchedule.User = _scheduleBLO.GetUserInfoByUserName(userName);
            var roomId = _scheduleBLO.GetRoomIdForBooking(searchSchedule);
            searchSchedule.RoomId = roomId;
            this.Session["SearchSchedule"] = searchSchedule;
            return RedirectToAction("Index", "CheckOut");
        }

        #endregion

        #region --- Manage Schedule ---

        public ActionResult ListSchedule()
        {
            ViewBag.CountRow = 0;
            var listScheduleInformation = _scheduleBLO.GetAllScheduleInfo();
            return View(listScheduleInformation);
        }

        /// <summary>
        /// Redirect to CreateSchedule page. Send ViewBag to load Combobox
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateSchedule()
        {
            ViewBag.Film = new SelectList(_filmBLO.LoadFilms(), "FilmId", "Name");
            ViewBag.Theater = new SelectList(_theaterBLO.GetAllTheaterInfo(), "TheaterId", "TheaterName");
            ViewBag.Promotion = new SelectList(_promotionBLO.LoadAllPromotions(), "PromotionId", "PromotionName");

            List<Room> listStrRoom = new List<Room>();
            ViewBag.Room = new SelectList(listStrRoom);

            List<ShowTime> listStrShowtime = new List<ShowTime>();
            ViewBag.ShowTime = new SelectList(listStrShowtime);

            return View();
        }

        /// <summary>
        /// Create a schedule film
        /// </summary>
        /// <param name="format"></param>
        /// <returns></returns>
        /// <author>vuongvt</author>
        /// <date>3/3/2014</date>
        [HttpPost]
        public ActionResult CreateSchedule(Schedule schedule)
        {
            if (ModelState.IsValid)
            {
                schedule.UserName = "vuongvt";
                _scheduleBLO.CreateSchedule(schedule);
                return RedirectToAction("ListSchedule");
            }
            else
            {
                ViewBag.Film = new SelectList(_filmBLO.LoadFilms(), "FilmId", "Name", schedule.FilmId);
                ViewBag.Theater = new SelectList(_theaterBLO.GetAllTheaterInfo(), "TheaterId", "TheaterName", schedule.TheaterId);
                ViewBag.Promotion = new SelectList(_promotionBLO.LoadAllPromotions(), "PromotionId", "PromotionName", schedule.PromotionId);

                List<Room> listStrRoom = new List<Room>();
                ViewBag.Room = new SelectList(listStrRoom);

                List<ShowTime> listStrShowtime = new List<ShowTime>();
                ViewBag.ShowTime = new SelectList(listStrShowtime);

                return View(schedule);
            }
        }


        public ActionResult EditSchedule(int id = 0)
        {
            Schedule schedule = _scheduleBLO.GetScheduleById(id);
            if (schedule == null)
            {
                return HttpNotFound();
            }
            ViewBag.Film = new SelectList(_filmBLO.LoadFilms(), "FilmId", "Name", schedule.FilmId);
            ViewBag.Theater = new SelectList(_theaterBLO.GetAllTheaterInfo(), "TheaterId", "TheaterName", schedule.TheaterId);
            ViewBag.Promotion = new SelectList(_promotionBLO.LoadAllPromotions(), "PromotionId", "PromotionName", schedule.PromotionId);
            ViewBag.Room = _roomBLO.GetSelectedListRoomByTheater(Convert.ToInt32(schedule.TheaterId));
            ViewBag.ShowTime = _showtimeBLO.GetSelectListShowTimeByTheater(Convert.ToInt32(schedule.TheaterId));
            return View(schedule);
        }

        [HttpPost]
        public ActionResult EditSchedule(Schedule schedule)
        {
            if (ModelState.IsValid)
            {
                _scheduleBLO.EditSchedule(schedule);
                return RedirectToAction("ListSchedule");
            }

            ViewBag.Film = new SelectList(_filmBLO.LoadFilms(), "FilmId", "Name", schedule.FilmId);
            ViewBag.Theater = new SelectList(_theaterBLO.GetAllTheaterInfo(), "TheaterId", "TheaterName", schedule.TheaterId);
            ViewBag.Promotion = new SelectList(_promotionBLO.LoadAllPromotions(), "PromotionId", "PromotionName", schedule.PromotionId);
            ViewBag.Room = _roomBLO.GetSelectedListRoomByTheater(Convert.ToInt32(schedule.TheaterId));
            ViewBag.ShowTime = _showtimeBLO.GetSelectListShowTimeByTheater(Convert.ToInt32(schedule.TheaterId));

            return View(schedule);
        }

        /// <summary>
        /// Delete a Schedule
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>vuongvt</author>
        /// <date>3/6/2014</date>
        [HttpDelete, ActionName("DeleteSchedule")]
        public ActionResult DeleteSchedule(int id = 0)
        {
            Schedule schedule = _scheduleBLO.GetScheduleById(id);
            if (schedule == null)
            {
                return HttpNotFound();
            }
            _scheduleBLO.DeleteSchedule(schedule);
            return RedirectToAction("ListSchedule");
        }

        /// <summary>
        /// Load combobox Room when choose a Theater
        /// </summary>
        /// <param name="theaterId"></param>
        /// <returns></returns>
        public JsonResult GetRoomByTheaterJson(int theaterId)
        {
            if (theaterId > 0)
            {
                return Json(_roomBLO.GetSelectedListRoomByTheater(theaterId));
            }
            else
            {
                List<string> listStrRoom = new List<string>();
                return Json(new SelectList(listStrRoom));
            }
        }

        /// <summary>
        /// Load combobox ShowTime when choose a Theater
        /// </summary>
        /// <param name="theaterId"></param>
        /// <returns></returns>
        public JsonResult GetShowTimeByTheaterJson(int theaterId)
        {
            if (theaterId > 0)
            {
                return Json(_showtimeBLO.GetSelectListShowTimeByTheater(theaterId));
            }
            else
            {
                List<ShowTime> listStrShowtime = new List<ShowTime>();
                return Json(new SelectList(listStrShowtime));
            }
        }

        #endregion
    }
}
